const { Sequelize, DataTypes } = require("sequelize")
const sequelize = new Sequelize('nodejs1', 'sa', '123456', {
    host: 'localhost',
    dialect: 'mssql'
})
try {
    sequelize.authenticate();
    console.log('Connection has been established successfully.');
} catch (error) {
    console.error('Unable to connect to the database:', error);
}
let Blog = sequelize.define('blogs',
    {
        'BName': {
            type: DataTypes.STRING,
            allowNull: false
        },
        'Contend': {
            type: DataTypes.STRING,
            allowNull: false
        },
        CatagoryId: {
            type: DataTypes.INTEGER,
            allowNull: false
        }
    }
)

let Catagory = sequelize.define('catagory',
    {
        Catagoryname: {
            type: DataTypes.STRING,
            allowNull: false
        }
    }
)


async function init() {
    sequelize.sync({ force: true }).then(
        async () => {
            let cate = await Catagory.create(
                {
                    Catagoryname: '你爹'
                }
            )
            Blog.bulkCreate(
                [{
                    BName: '李达',
                    Contend: '沙雕',
                    CatagoryId: cate.id
                }]
            )

        }
    )
}

const koa = require("koa")

const router = require("koa-router")()

router.get('/blog',
    async (ctx) => {
        let keyword = ctx.request.query.keyword || '';
        if (keyword) {
            let list = await Blog.findAll({
                where: {
                    [Sequelize.Op.or]: [
                        { id: isNaN(keyword) ? 0 : parseInt(keyword) },
                        { BName: keyword },
                        { Contend: keyword }
                    ]
                }
            })
            ctx.body = {
                ctx: 1000,
                msg: '查询成功',
                data: list
            }
        } else {
            let b = await Blog.findAll();
            let c = await Catagory.findAll();
            console.log(b);
            ctx.body = {
                code: 1000,
                msg: "获取博客成功",
                data: b,
            }
        }


    }
).post('/blog', async (ctx) => {
    let obj = ctx.request.body;
    await Blog.create(obj);
    ctx.body = obj;
}).put('/blog/:id', async (ctx) => {
    let par = ctx.request.params.id
    let obj = ctx.request.body
    await Blog.update(
        obj, {
        where: {
            id: par
        }
    }
    )
    ctx.body = {
        code: 1000,
        msg: "更新博客成功",
        data: obj
    }
}).get('/blog/:id', async (ctx) => {
    let id = ctx.request.params.id;
    let blog = await Blog.findByPk(id)
    if (blog) {
        ctx.body = {
            code: 1000,
            data: blog,
            msg: 'aaa'
        }
    } else {
        ctx.body = {
            code: 900,
            data: '',
            msg: 'bbb'
        }
    }
}).delete('/blog/:id', async (ctx) => {
    let id = ctx.request.params.id;
    await Blog.destroy(
        {
            where: {
                id: id
            }
        }
    );
    ctx.body = {
        code: 1000,
        msg: '删除成功'
    }
})
init();
const body = require("koa-bodyparser");
const cors = require("koa-cors")
let app = new koa();
app.use(body()).use(cors())
app.use(router.routes())
let port = 8000;
app.listen(port)
console.log(`此服务器运行在:http://localhost:${port}`);